<?php

class Admin_articleController extends Admin_baseController {
    
    public function listAction() {
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $where = $value = $url = array();
        $title = Request::getParams('title');
        $status = Request::getParams('status', -1);
        $categoryId = Request::getParams('categoryId');
        
        if ($title) {
            $where [] = 'title like ?';
            $value [] = '%' . $title . '%';
            $url ['title'] = $title;
        }
        if ($status!=-1) {
            $where [] = 'status=?';
            $value [] = $status;
            $url ['status'] = $status;
        }
        if($categoryId) {
            $where [] = 'category_id=?';
            $value [] = $categoryId;
            $url ['categoryId'] = $categoryId; 
        }
        $total = self::model('bbs_article')->count(null, $where, $value);
        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'title' => $title,
            'total' => $total,
            'status' => $status,
            'categoryId' => $categoryId,
            'categoryList' => self::model('bbs_category')->getCategoryListByCId(),
            'statusList' => self::model('bbs_article')->getStatusList(),
            'list' => self::model('bbs_article')->limit(($page - 1) * $pageSize, $pageSize)->order('article_id desc')->getAll($where, $value),
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl().'/admin_article/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml()
        ));
        $this->display();
    }

    public function editAction() {
        $article_id = Request::getParams('articleId');
        $data = [];
        $data['title'] = Request::getParams('title');
        $data['image_list'] = Request::getParams('image_list');
        $data['category_id'] = Request::getParams('categoryId');
        $data['content'] = Html::decode(Request::getParams('content'));
        $data['status'] = intval(Request::getParams('status', 1));
        $data['sort'] = intval(Request::getParams('sort', 0));
        $data['is_recommend'] = intval(Request::getParams('is_recommend', 0));
        $data['is_top'] = intval(Request::getParams('is_top', 0));
        $data['update_time'] = time();
        
        $article_info = [];
        if ($article_id && !$article_info = self::model('bbs_article')->getOne('article_id=?', $article_id)) {
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if (Request::isGet()) {
            $this->assign('articleInfo', $article_info);
            $this->display();
            exit;
        }
//        $result = articleModel::validata($data);
//        if($result!==true) {
//            Response::ajax(NORMAL_ERROR, $result);
//        }
        if($data['image_list']) {
            $data['image_list'] = implode(';', $data['image_list']);
        }
        if ($article_id) {//修改
            if (self::model('bbs_article')->update($data, 'article_id=?', $article_id)) {
                Response::ajax(200, '修改成功');
            }
        } else {//新增
            $data['add_time'] = time();
            if (self::model('bbs_article')->insert($data)) {
                Response::ajax(200, '添加成功');
            }
        }
        Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
    }

    public function deleteAction() {
        $article_id = intval(Request::getParams('articleId'));
        if (self::model('bbs_article')->delete('article_id=?', $article_id)) {
            Response::ajax(200, '删除成功');
        }
        Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
    }

    public function categoryAction() {
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $where = $value = $url = array();
        $where[] = 'status=?';
        $value[] = 1;
        $name = Request::getParams('name');
        if ($name) {
            $where [] = 'name like ?';
            $value [] = '%' . $name . '%';
            $url ['nickname'] = $name;
        }
        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'name' => $name,
            'list' => self::model('bbs_category')->order('sort asc')->getAll($where, $value)
        ));
        $this->display();
    }

    public function categoryEditAction() {
        $categoryId = Request::getParams('categoryId');
        $data = [];
        $data['name'] = Request::getParams('name');
        $data['icon'] = Request::getParams('icon');
        $data['sort'] = intval(Request::getParams('sort'));
        $data['update_time'] = time();
        $categoryInfo = [];
        if ($categoryId && !$categoryInfo = self::model('bbs_category')->getOne('id=?', $categoryId)) {
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if (Request::isGet()) {
            $this->assign('categoryInfo', $categoryInfo);
            $this->display();
            exit;
        }
        if ($categoryId) {//修改
            if (self::model('bbs_category')->update($data, 'id=?', $categoryId)) {
                Response::ajax(200, '修改成功');
            }
        } else {//新增
            $data['add_time'] = time();
            if (self::model('bbs_category')->insert($data)) {
                Response::ajax(200, '添加成功');
            }
        }
        Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
    }
    
    public function categoryDeleteAction() {
        $categoryId = Request::getParams('categoryId');
        if (!$categoryId || !$categoryInfo = self::model('bbs_category')->getOne('id=?', $categoryId)) {
            Response::ajax(NORMAL_ERROR, '信息查询失败,删除失败');
        }
        if(self::model('bbs_category')->update([
            'status'=> 0
        ], 'id=?', $categoryId)) {
            Response::ajax(200, '删除成功');
        }
        Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
    }

    public function categoryStatusAction() {
        
    }

}
